<?php
/**
 * Created by PhpStorm.
 * User: hpj
 * Date: 2018/10/17
 * Time: 23:17
 */
namespace App\Chat;

use Exception;
use \GlobalData\Client;
use \GatewayWorker\Lib\Gateway;
use mysqli;

class User
{
    const VISITOR = 'visitor';
    const ADMIN = 'admin';

    public static $users = [];

    public static $num = 0;

    public static $dataClient = null;

    public static function getInstance(){
        if(self::$dataClient==null){
            return self::$dataClient = new Client('127.0.0.1:2207');
        }
        return self::$dataClient;
    }
    public static function setConnectNum($num=1){
	    $client = self::getInstance();
	    if(isset($client->connectNum)){
	    	$client->connectNum +=$num;
	    }else{
	    	$client->connectNum = 0 ;
	    }
    }

    public static function getConnectNum(){
    	$client = self::getInstance();
	$num = $client->connectNum;
	return $num;

    }
    //读取用户
    public static function getUsers(){
        return self::getInstance()->users;
    }
    //存储用户
    public static function setUsers($user){
        $users = self::getInstance()->users;
        $users[] =  $user;
        self::getInstance()->users = $users;
    }
    //删除用户
    public static function delUsers($clientId){
        $users = self::getInstance()->users;
    }
    //用户登入
    public static function login($client_id,$message){
        try {
            $room_id = $message['room_id'];
            $client_name = htmlspecialchars($message['client_name']);
            $avatar = htmlspecialchars($message['avatar']);
            $role = $message["role"];
            $_SESSION['room_id'] = $room_id;
            $_SESSION['client_name'] = $client_name;
            $_SESSION['avatar'] = $avatar;
            $_SESSION['role'] = $role;

            Gateway::joinGroup($client_id,$room_id);
            self::updateGroupClientInfo($room_id);
        }catch (Exception $e){
            error_log("login fail".$e->getMessage(),1,"240088298@qq.com");
            return false;
        }
        return true;
    }

    public static function updateGroupClientInfo($room){
        $client = self::getInstance();
        $clients_list = Gateway::getClientSessionsByGroup($room);
        $allGroup = $client->all_group;
        //var_dump($clients_list);
        $allGroup[$room] = $clients_list;
        //var_dump($allGroup);
        //$clients_list = [1=>[2=>[3],4=>12]];
        //var_dump($clients_list);
        $client->all_group = $allGroup;
        //var_dump($client->all_group);
    }

    //获取网站用户列表,根据类型
    public static function getUserList($role,$room_id){
        $clients_list = Gateway::getClientSessionsByGroup($room_id);

        foreach($clients_list as $tmp_client_id=>$item)
        {
            switch ($role){
                case self::VISITOR:
                    if($item['role'] != self::VISITOR){
                        unset($clients_list[$tmp_client_id]);
                    }
                    break;
                case self::ADMIN:
                    if($item['role'] != self::ADMIN){
                        unset($clients_list[$tmp_client_id]);
                    }
                    break;
            }
        }

        return $clients_list;
    }

    //第一次过滤
    static function Filter1(&$msg,&$isOk){
        if ($msg == "Filter1"){
            echo "!2312312;";
            $msg = "change Filter2";
            $isOk = true;
        }
    }

    //第二次过滤
    static function Filter2(&$msg,&$isOk){
        if ($msg == "Filter2"){
            $msg = " change Filter2";
            $isOk = true;
        }
    }
    static function DoMsgFilter(&$msg,&$isAutoReplay,$siteId  = 0){
        var_dump("debug---------",$siteId);
        $isAutoReplay = true;
        $msg = "debug";
        return;
         if ($roomId){
             $host = "127.0.0.1";
             $username = "jmu";
             $passwd = "jmu";
             $dbname = "aci";

             $mysql = new mysqli($host ,
                 $username ,
                 $passwd ,
                 $dbname );

             $sql = "select  `match`,resepons_msg from t_aci_filter where  site_id = $siteId order by create_time";
             $rows = $mysql->query($sql);
             $data = [];
             while ($row =$rows->fetch_assoc()){
                 $data [] =$row;
             }
            foreach ($data as $item){
                $key = $item["match"];
                $respone = $item["resepons_msg"];
                if (strpos($msg,$key) !==false){
                    $isAutoReplay = true;
                    $msg = $respone;
                    return ;
                }
            }
         }
    }

    static function MysqlInit(){
        $init =  mysqli_init();

    }



}
